﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Messaging;
using System.Text;

public partial class backend_Menu : System.Web.UI.Page
{
    public int MenuID
    {
        get
        {
            return Convert.ToInt32(ViewState["MenuID"]);
        }
        set
        {
            ViewState["MenuID"] = value;
        }
    }

    public int CheckAddOrEdit
    {
        get
        {
            return Convert.ToInt32(ViewState["CheckAddOrEdit"]);
        }
        set
        {
            ViewState["CheckAddOrEdit"] = value;
        }
    }

    MenuManagement MyMenu = new MenuManagement();
    MenuDB MyMenuDB = new MenuDB();
    AccountManagement MyAccount = new AccountManagement();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Constants.SESSION_ACCOUNTID].ToString() == string.Empty)
                return;
            if (Session[Constants.SESSION_ACCOUNTTYPE].ToString() != ((int)EnumAccountType.Admin).ToString() &&
                (Session[Constants.SESSION_ACCOUNTTYPE].ToString() != ((int)EnumAccountType.RootAdmin).ToString()))
            {
                try
                {
                    Response.Redirect(Request.UrlReferrer.ToString());
                }
                catch
                {
                    Response.Redirect("/Default/Index.html");
                }
            }
            LoadTreeMenu();
            BinDataForDropType();
        }
    }

    private void LoadTreeMenu()
    {
        var RootNode = new RadTreeNode("Trang chủ VTCEdu", "0");
        RadTreeViewMenu.Nodes.Add(RootNode);
        RootNode.BackColor.G.ToString();
        AddNodes(RadTreeViewMenu.Nodes[0].Nodes, 0);
        RadTreeViewMenu.ExpandAllNodes();
    }

    private void SetControls(bool edit)
    {
        lblUrl.Visible = edit;
        txtUrl.Visible = edit;
    }

    private void SetControlsRdb(bool edit)
    {
        rdbActive.Checked = edit;
        rdbNoActive.Checked = !edit;
    }

    private void SetControlsEdit(bool edit,string name)
    {
        CtrEdit.Visible = edit;
        lblMsg1.Visible = !edit;
        btnSave.Text = name;
    }

    private void Clear()
    {
        txtMenuName.Text = string.Empty;
        txtOrder.Text = string.Empty;
        txtUrl.Text = string.Empty;
    }

    private void AddNodes(RadTreeNodeCollection radTreeMenu, int menuParentID)
    {
        DataTable ListMenu = MyMenu.GetMenuChild(menuParentID);
        foreach (DataRow Menu in ListMenu.Rows)
        {
            var Node = new RadTreeNode(Menu["Name"].ToString(), Menu["MenuID"].ToString());
            radTreeMenu.Add(Node);
            AddNodes(Node.Nodes, Convert.ToInt32(Menu["MenuID"]));
        }
    }

    private void BinDataForDropType()
    {
        ddlMenuType.Items.Add(new ListItem("Nhóm tin tức", Convert.ToInt32(EnumMenuType.GroupNews).ToString()));
        ddlMenuType.Items.Add(new ListItem("Nhóm Menu", Convert.ToInt32(EnumMenuType.GroupMenu).ToString()));
        ddlMenuType.Items.Add(new ListItem("Tin tức", Convert.ToInt32(EnumMenuType.News).ToString()));
        ddlMenuType.Items.Add(new ListItem("Phòng ban", Convert.ToInt32(EnumMenuType.Department).ToString()));
        ddlMenuType.Items.Add(new ListItem("Tài nguyên", Convert.ToInt32(EnumMenuType.Resource).ToString()));
        ddlMenuType.Items.Add(new ListItem("Nhân sự", Convert.ToInt32(EnumMenuType.Employee).ToString()));
        ddlMenuType.Items.Add(new ListItem("Liên kết ngoài", Convert.ToInt32(EnumMenuType.Link).ToString()));
    }

    protected void RadTreeViewMenu_ContextMenuItemClick(object sender, Telerik.Web.UI.RadTreeViewContextMenuEventArgs e)
    {
        MenuInfo MyMenuInfo = MyMenuDB.GetInfo(Convert.ToInt32(e.Node.Value));

        switch (e.MenuItem.Value)
        {
            case "Add":
                if (Convert.ToInt32(MyAccount.GetInfo(Convert.ToInt32(Session[Constants.SESSION_ACCOUNTID])).Type) == Convert.ToInt32(EnumAccountType.RootAdmin) || Convert.ToInt32(MyAccount.GetInfo(Convert.ToInt32(Session[Constants.SESSION_ACCOUNTID])).Type) == Convert.ToInt32(EnumAccountType.Admin))
                {
                    CheckAddOrEdit = 1;
                    txtMenuName.Focus();
                    SetControlsEdit(true, "Lưu lại");
                    SetControls(false);
                    Clear();
                    MenuID = Convert.ToInt32(e.Node.Value);
                    ddlMenuType.Items.Clear();
                    SetControlsRdb(false);
                    BinDataForDropType();
                }
                else
                {
                    lblMsg1.Text = "  <img src='/images/attention.ico' width='20px' height='20px' /> Bạn không được quyền sử dụng chức năng này  ";
                    lblMsg1.Visible = true;
                }
                break;
            case "Edit":
                if (Convert.ToInt32(MyAccount.GetInfo(Convert.ToInt32(Session[Constants.SESSION_ACCOUNTID])).Type) == Convert.ToInt32(EnumAccountType.RootAdmin) || Convert.ToInt32(MyAccount.GetInfo(Convert.ToInt32(Session[Constants.SESSION_ACCOUNTID])).Type) == Convert.ToInt32(EnumAccountType.Admin))
                {
                    CheckAddOrEdit = 0;
                    txtMenuName.Focus();
                    SetControlsEdit(true, "Cập nhật");
                    MenuID = Convert.ToInt32(e.Node.Value);
                    txtMenuName.Text = HtmlUtility.HtmlDecode(MyMenuInfo.Name.ToString());
                    txtOrder.Text = MyMenuInfo.Order.ToString();
                    if (MyMenuInfo.Status == Convert.ToInt32(EnumStatus.Active))
                    {
                        SetControlsRdb(true);
                    }
                    else
                    {
                        SetControlsRdb(false);
                    }
                    if (MyMenuInfo.Type != Convert.ToInt32(EnumMenuType.Link))
                    {
                        SetControls(false);
                    }
                    else
                    {
                        SetControls(true);
                        txtUrl.Text = HtmlUtility.HtmlDecode(MyMenuInfo.Url);
                    }
                    ddlMenuType.SelectedValue = MyMenuInfo.Type.ToString();
                }
                else
                {
                    lblMsg1.Text = "  <img src='/images/attention.ico' width='20px' height='20px' /> Bạn không được quyền sử dụng chức năng này  ";
                    lblMsg1.Visible = true;
                }
                break;
            case "Delete":
                if (Convert.ToInt32(MyAccount.GetInfo(Convert.ToInt32(Session[Constants.SESSION_ACCOUNTID])).Type) == Convert.ToInt32(EnumAccountType.RootAdmin) || Convert.ToInt32(MyAccount.GetInfo(Convert.ToInt32(Session[Constants.SESSION_ACCOUNTID])).Type) == Convert.ToInt32(EnumAccountType.Admin))
                {
                    CtrEdit.Visible = false;
                    MenuID = Convert.ToInt32(e.Node.Value);
                    DataTable MenuChild = MyMenu.GetMenuChild(Convert.ToInt32(e.Node.Value));
                    if (MenuChild.Rows.Count == 0)
                    {
                        try
                        {
                            MyMenu.DeleteMenuByMenuID(MenuID);
                        }
                        catch
                        {
                            lblMsg1.Visible = true;
                            return;
                        }
                        Response.Redirect("~/backend/Menu.aspx");
                    }
                    else
                    {
                        lblMsg1.Text = "  <img src='/images/attention.ico' width='20px' height='20px' /> Bạn không được xóa Menu này  ";
                        lblMsg1.Visible = true;
                    }
                }
                else
                {
                    lblMsg1.Text = "  <img src='/images/attention.ico' width='20px' height='20px' /> Bạn không được quyền sử dụng chức năng này  ";
                    lblMsg1.Visible = true;
                }
                break;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Clear();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (CheckAddOrEdit == 1)
        {
            MenuInfo MenuInfo = new MenuInfo();
            MenuInfo.Name = HtmlUtility.HtmlEncode(txtMenuName.Text.Trim());
            MenuInfo.Type = Convert.ToInt32(ddlMenuType.SelectedValue);
            MenuInfo.Order = Convert.ToInt32(txtOrder.Text);
            MenuInfo.ParentID = MenuID;
            if (MenuInfo.Type != Convert.ToInt32(EnumMenuType.Link))
            {
                if (MenuInfo.Type == Convert.ToInt32(EnumMenuType.GroupMenu))
                {
                    MenuInfo.Url = "#";
                }
                else
                    MenuInfo.Url = string.Empty;
            }
            else
            {
                MenuInfo.Url = HtmlUtility.HtmlEncode(txtUrl.Text.Trim());
            }
            if (rdbActive.Checked)
            {
                MenuInfo.Status = Convert.ToInt32(EnumStatus.Active);
            }
            if (rdbNoActive.Checked)
            {
                MenuInfo.Status = Convert.ToInt32(EnumStatus.InActive);
            }
            MyMenu.InsertMenu(MenuInfo);
            Response.Redirect("~/backend/Menu.aspx");
        }
        else
        {
            MenuInfo MenuInfo = MyMenuDB.GetInfo(MenuID);
            MenuInfo.Name = HtmlUtility.HtmlEncode(txtMenuName.Text.Trim());
            MenuInfo.Type = Convert.ToInt32(ddlMenuType.SelectedValue);
            MenuInfo.Order = Convert.ToInt32(txtOrder.Text);
            if (MenuInfo.Type == Convert.ToInt32(EnumMenuType.Link))
            {
                MenuInfo.Url = HtmlUtility.HtmlEncode(txtUrl.Text.Trim());
            }
            else
            {
                if (MenuInfo.Type == Convert.ToInt32(EnumMenuType.GroupMenu))
                {
                    MenuInfo.Url = "#";
                }
                else
                    MenuInfo.Url = string.Empty;
            }
            if (rdbActive.Checked)
            {
                MenuInfo.Status = Convert.ToInt32(EnumStatus.Active);
            }
            if (rdbNoActive.Checked)
            {
                MenuInfo.Status = Convert.ToInt32(EnumStatus.InActive);
            }
            MyMenu.UpdateMenuByMenuID(MenuInfo);
            Response.Redirect("~/backend/Menu.aspx");
        }
    }

    protected void ddlMenuType_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (ddlMenuType.SelectedValue == Convert.ToInt32(EnumMenuType.Link).ToString())
        {
             SetControls(true);
        }
        else
        {
             SetControls(false);
        }
    }

    protected void btnClose_Click(object sender, EventArgs e)
    {
         CtrEdit.Visible = false;
    }
}
